#include "app_tasks.h"
#include "app_motor_pwm.h"
#include "app_ui_lcd.h"

// UI状态监控任务相关变量
static u32 last_motor_duty = 0xFFFFFFFF;  // 用于检测占空比变化
static u8 last_motor_status = 0xFF;       // 用于检测运行状态变化

/**
 * @brief 电压显示更新任务
 * 并在LCD上显示电压数字（格式：XX.X V）
 */
void voltage_display_task(void *pvParameters)
{
    (void)pvParameters;

    printf("[Voltage Display] Task started\n");

    while (1) {
        // 直接传入 0 让 LCD 函数自己获取电压值
        ui_draw_mu(0);  // 传入 0 表示使用默认的 PWM 电压计算

        // 每500ms更新一次电压显示
        os_time_dly(50);  // 50 * 10ms = 500ms
    }
}

/**
 * @brief UI状态监控任务
 * 确保UI显示与实际电机状态保持同步
 */
void ui_monitor_task(void *pvParameters)
{
    (void)pvParameters;

    while (1) {
        u32 current_duty = app_motor_get_duty();
        u8 current_status = app_motor_get_status();

        // 添加调试信息
        static u32 debug_counter = 0;
        if (debug_counter % 10 == 0) {  // 每1秒打印一次
            printf("[UI Monitor] current_duty=%d, current_status=%d, last_duty=%d, last_status=%d\n", 
                   current_duty, current_status, last_motor_duty, last_motor_status);
        }
        debug_counter++;

        // 检查占空比是否发生变化
        if (current_duty != last_motor_duty) {
            printf("[UI Monitor] Duty changed: %d -> %d\n", last_motor_duty, current_duty);

            // 更新UI显示
            ui_draw_status(current_status);

            last_motor_duty = current_duty;
        }

        // 检查运行状态是否发生变化
        if (current_status != last_motor_status) {
            printf("[UI Monitor] Status changed: %s -> %s\n",
                   last_motor_status ? "Running" : "Stopped",
                   current_status ? "Running" : "Stopped");

            // 更新UI显示
            ui_draw_status(current_status);

            last_motor_status = current_status;
        }

        // 每100ms检查一次状态
        os_time_dly(50);  // 50 * 10ms = 500ms
    }
}

/**
 * @brief 转速显示更新任务
 
 * 然后调用ui_draw_hz()函数进行显示更新
 */
void speed_display_task(void *pvParameters)
{
    (void)pvParameters;

    printf("[Speed Display] Task started\n");

    while (1) {
        // u32 actual_speed = your_speed_sensor_function();  // 替换为实际的转速获取函数
        // 临时使用固定测试值进行测试
        u32 test_speed = 157;  // 模拟转速值
        
        // 更新转速显示
        ui_draw_hz(test_speed);
        
        // 每200ms更新一次转速显示
        os_time_dly(100);  // 100 * 10ms = 1000ms
    }
}

/**
 * @brief 创建所有应用任务
 * 使用系统原生的任务创建接口
 */
void app_tasks_create_all(void)
{
    printf("[App Tasks] Creating all application tasks...\n");

    // 创建UI监控任务
    int ret1 = os_task_create(ui_monitor_task, NULL, 10, 512, 0, "ui_monitor");
    if (ret1 == OS_NO_ERR) {
        printf("[App Tasks] UI monitor task created successfully\n");
    } else {
        printf("[App Tasks] Failed to create UI monitor task, error: %d\n", ret1);
    }

    // 创建电压显示任务
    int ret2 = os_task_create(voltage_display_task, NULL, 8, 256, 0, "voltage_display");
    if (ret2 == OS_NO_ERR) {
        printf("[App Tasks] Voltage display task created successfully\n");
    } else {
        printf("[App Tasks] Failed to create voltage display task, error: %d\n", ret2);
    }

    // 创建转速显示任务
    int ret3 = os_task_create(speed_display_task, NULL, 9, 256, 0, "speed_display");
    if (ret3 == OS_NO_ERR) {
        printf("[App Tasks] Speed display task created successfully\n");
    } else {
        printf("[App Tasks] Failed to create speed display task, error: %d\n", ret3);
    }

    printf("[App Tasks] All application tasks creation completed\n");
}

